package online.sixfish.doubao.controller.admin;

import online.sixfish.common.api.ApiResult;
import online.sixfish.common.dto.PageDTO;
import online.sixfish.question.model.entity.QueProgram;
import online.sixfish.question.service.QueProgramService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

@RestController
@CrossOrigin
@RequestMapping("/admin/question")
public class AdminQuestionController {
    @Resource
    QueProgramService queProgramService;

    @PostMapping("/program/upload")
    @PreAuthorize("hasRole('admin')")
    public ApiResult upload(MultipartFile file) throws IOException {
        queProgramService.upload(file);
        return ApiResult.success();
    }

    @PostMapping("/program/delete/{id}")
    @PreAuthorize("hasRole('admin')")
    public ApiResult delete(@PathVariable Integer id) {
       queProgramService.delete(id);
        return ApiResult.success();
    }

    @GetMapping("/program/list")
    @PreAuthorize("hasRole('admin')")
    public ApiResult list(@RequestParam(required = false) Integer difficulty,
                          @RequestParam(required = false) Integer sortType,
                          @RequestParam(required = false) String name,
                          @RequestParam Integer pageNum,
                          @RequestParam Integer pageSize) {
        PageDTO<QueProgram> pageDTO = queProgramService
                .select(difficulty, sortType, name, pageNum, pageSize);
        return ApiResult.success(pageDTO);
    }


}
